﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace GestaltDesigner
{
    public partial class ToolBox : UserControl
    {
        private ObservableCollection<ToolBoxCategory> _categories;

        public event EventHandler<ToolBoxItemChosenEventArgs> ItemChosen;

        public ToolBox()
        {
            InitializeComponent();
            _categories = new ObservableCollection<ToolBoxCategory>();
            _categories.CollectionChanged += new NotifyCollectionChangedEventHandler(_categories_CollectionChanged);
        }

        void _categories_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // TODO: finish the other actions here
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                ProcessCategory(e.NewItems[0] as ToolBoxCategory);
            }
        }

        private void ProcessCategory(ToolBoxCategory category)
        {
            TextBlock itemText;
            foreach (KeyValuePair<string, string> kvp in category.Types)
            {
                itemText = new TextBlock();
                itemText.Text = kvp.Key;
                itemText.Tag = kvp.Value;
                itemText.Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0xc2, 0xc2, 0xc2));
                itemText.FontSize = 11;
                _itemsContainer.Children.Add(itemText);
                itemText.MouseLeftButtonUp += new MouseButtonEventHandler(itemText_MouseLeftButtonUp);
            }
        }

        void itemText_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ItemChosen != null)
            {
                ItemChosen(this, new ToolBoxItemChosenEventArgs((sender as TextBlock).Text, (sender as TextBlock).Tag as string));
            }
        }

        public ObservableCollection<ToolBoxCategory> Categories
        {
            get
            {
                return _categories;
            }
        }
    }

    public class ToolBoxItemChosenEventArgs : EventArgs
    {
        private string _name;
        private string _type;

        public ToolBoxItemChosenEventArgs(string name, string type)
        {
            _name = name;
            _type = type;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public string Type
        {
            get
            {
                return _type;
            }
        }
    }

    public class ToolBoxCategory
    {
        private string _title;
        private Dictionary<string, string> _types;

        public ToolBoxCategory(string title, Dictionary<string, string> types)
        {
            _title = title;
            _types = types;
        }

        public string Title
        {
            get
            {
                return _title;
            }
        }

        public Dictionary<string, string> Types
        {
            get
            {
                return _types;
            }
        }
    }
}
